Skip to content

Match bubble sort C implementation with Julia #254

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 17, 2018

Conversation

mika314
Copy link
Contributor

@mika314 mika314 commented Jul 16, 2018

No description provided.

@Butt4cak3 Butt4cak3 added the Implementation Edit This provides an edit to an algorithm implementation. (Code and maybe md files are edited.) label Jul 16, 2018
@@ -1,42 +1,36 @@
#include <stdio.h>
#include <stddef.h>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know I did it but you don't need that library. size_t i in stdio.h

Copy link
Contributor Author

@mika314 mika314 Jul 16, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

extra include is removed.

}
}

int main() {
int array[10] = {1, 45, 756, 4569, 56, 3, 8, 5, -10, -4};
const int N = 10;
int array[N] = {1, 45, 756, 4569, 56, 3, 8, 5, -10, -4};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code doesn't run because of the N here. You need just put int array[10] = ....

Copy link
Contributor

@Butt4cak3 Butt4cak3 Jul 16, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couldn't you just use int array[] = { ... }? I tried that and got no errors whatsoever. You could then determine the value of N with sizeof.

Disclaimer: I'm not a C programmer.

Copy link
Contributor

@Butt4cak3 Butt4cak3 Jul 16, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, and also, wouldn't size_t be more fitting because it describes the size of the array?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can do both. I would change int to size_t, I just didn't notice. On the first point, use int array[] = { ... }.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My bad, I forgot it is C, I need to do #define

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think deriving N using sizeof is better than doing #define. It make code dryer, with #define, information about size of array used independently in 2 places: it is embedded in declaration of array and in #define, in case of sizeof it is only in the array.

I re-pushed changes with sizeof.

}
}

int main() {
int array[10] = {1, 45, 756, 4569, 56, 3, 8, 5, -10, -4};
int array[] = {1, 45, 756, 4569, 56, 3, 8, 5, -10, -4};
size_t N = sizeof(array) / sizeof(*array);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just have size_t N = 10;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will make code less stable, if you make a mistake and assign 100 to N it
will crash.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm asking you to set N to 10, that will not create any issues.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Gathros That's reduntant, though. There is only one correct value for N and that's the length of the array. You can calculate that, so why hardcode it? That way, you can add/remove numbers from the array without having to worry about changing N, too.

@Gathros Gathros merged commit 8b0e8f1 into algorithm-archivists:master Jul 17, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Implementation Edit This provides an edit to an algorithm implementation. (Code and maybe md files are edited.)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants